package com.zjitc.service.imp;/**
 * Create by IntelliJ IDEA
 *
 * @author: MaoYongQi
 * @date-Time: 2018/4/2 10:24
 * @description:
 */

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zjitc.common.ServerResponse;
import com.zjitc.dao.ProductDao;
import com.zjitc.model.Product;
import com.zjitc.model.User;
import com.zjitc.service.ProductService;
import javax.servlet.http.HttpSession;

import com.zjitc.utils.Conster;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Create by IntelliJ IDEA
 *
 * @author: MaoYongQi
 * @date-Time: 2018/4/2 10:24
 * @description:
 */
@Service
public class ProduceServiceImpl implements ProductService {

  @Autowired
  private ProductDao dao;

  @Override
  public ServerResponse<PageInfo<Product>> findByList(Integer pageNum, Integer pageSize,
      String orderBy) {
    try {
      PageHelper.startPage(pageNum, pageSize);
      List<Product> productList = dao.findProductOrderBy(orderBy);
      PageInfo info = new PageInfo(productList);
      return ServerResponse.createSuccess(info);
    } catch (Exception e) {
      return ServerResponse.createError("参数错误");
    }
  }

  @Override
  public ServerResponse<Product> findDetail(Integer productId) {
    try {
      Product product = dao.finddetail(productId);
      return ServerResponse.createSuccess(product);
    } catch (Exception e) {
      return ServerResponse.createError("该商品已下架或删除");
    }
  }

  /**
   * 产品list
   */
  @Override
  public ServerResponse<Product> findAllByList(Integer pageNum, Integer pageSize,
      HttpSession session) {
    User backUser = (User) session.getAttribute(Conster.BACKUSER);
    if(backUser == null){
      return ServerResponse.createBackError("用户未登录,请登录");
    }
    PageHelper.startPage(pageNum,pageSize);
    List<Product> productList = dao.findAllProduct();
    PageInfo info = new PageInfo(productList);
    return ServerResponse.createSuccess(info);
  }

  /**
   * 产品搜索
   */
  @Override
  public ServerResponse<Product> serach(String productName, String productId, Integer pageNum,
      Integer pageSize, HttpSession session) {
    User backUser = (User) session.getAttribute(Conster.BACKUSER);
    if(backUser == null){
      return ServerResponse.createBackError("用户未登录,请登录");
    }
    try {
      PageHelper.startPage(pageNum, pageSize);
      List<Product> productList = dao.findByNameAndID(productName, productId);
      PageInfo info = new PageInfo(productList);
      return ServerResponse.createSuccess(info);
    }catch (Exception e){
      e.printStackTrace();
      return ServerResponse.createError("出错了");
    }
  }

  @Override
  public ServerResponse<Product> setStatus(Integer productId, Integer status) {
    try{
      dao.updateStatusById(productId,status);
      return ServerResponse.createSuccess("修改产品状态成功");
    }catch (Exception e){
      e.printStackTrace();
      return ServerResponse.createError("修改产品状态失败");
    }
  }

  @Override
  public ServerResponse<Product> update(Product product) {
    try {
      if(product.getProductId() != null){
        dao.addProduct(product);
        return ServerResponse.createSuccess("新增产品成功");
      }
      dao.updateProduct(product);
      return ServerResponse.createSuccess("更新产品成功");
    }catch (Exception e){
      e.printStackTrace();
      return ServerResponse.createError("更新产品失败");
    }
  }
}
